package com.xone.xapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

import com.xone.refresh.library.PullToRefreshListView;

public class ProductListActivity extends BasicListActivity<ProductListHolder> {

	protected String TAG = ProductListActivity.class.getSimpleName();
	protected ProductAdapter productAdapter;
	protected PullToRefreshListView listView;
	protected String saleType;
	protected String title;
	protected AutoCompleteTextView autoCompleteTextView;
	protected Bundle params;
	protected List<NameValuePair> requestParamters = new ArrayList<NameValuePair>();
	public String getSaleType() {
		return saleType;
	}
	public void setSaleType(String saleType) {
		this.saleType = saleType;
	}
	@Override
	public int getContentView() {
		return R.layout.product_list;
	}
	@Override
	public String getHeaderTitle() {
		return title;
	}
	/**
	 * 是否需要检测登录
	 * @return
	 */
	@Override
	public boolean isLoginCheck() {
		return false;
	}
	/**
	 * 列表视图
	 */
	@Override
	public PullToRefreshListView getListView() {
		return listView;
	}
	/**
	 * 列表适配器
	 */
	@Override
	public BasicListAdapter<ProductListHolder> getListAdapter(Context context,
			List<JSONObject> result) {
		return new ProductAdapter(context, result);
	}
	/**
	 * 列表的请求地址
	 */
	@Override
	public String getRequestURL() {
		return "/product/listJsonItems.html";
	}
	/**
	 * 列表的请求参数
	 */
	@Override
	public List<NameValuePair> getRequestParams() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("product.saleType", saleType));
		if (null != requestParamters && !requestParamters.isEmpty()) {
			params.addAll(requestParamters);
		}
		return params;
	}
	/**
	 * 返回的请求中的json根列表名称
	 */
	@Override
	public String getJsonRootName() {
		return "list";
	}
	@Override
	protected void rightButton(Button rightButton) {
		rightButton.setText("更多");
		rightButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivitytoAutoComplete();
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Bundle bundle = this.getIntent().getExtras();
		if (null != bundle) {
			saleType = bundle.getString("saleType");
			if (StringUtils.isBlank(saleType)) {
				Toast.makeText(this, "无产品类型信息", Toast.LENGTH_SHORT).show();
				this.finish();
				return;
			}
		}
		if ("0".equals(saleType)) {
			title = "普通产品";
		} else if ("1".equals(saleType)) {
			title = "促销产品";
		} else if ("2".equals(saleType)) {
			title = "组团产品";
		}
		super.onCreate(savedInstanceState);
		if (!ContextUtils.isOnline(this)) {
			return;
		}
		listView = (PullToRefreshListView) findViewById(R.id.product_list);
		showProgress();
		new InitDataTask(this, listView).execute(getRequestParams());
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
	
	/**
	 * 打开到另外一个活动
	 */
	protected void startActivitytoAutoComplete() {
		Intent i = new Intent(this, AutoCompleteActivity.class);
		i.putExtra("saleType", saleType);
		if (null != params) {
			for (String key : params.keySet()) {
				i.putExtra(key, params.getString(key));
			}
		}
		startActivityForResult(i, 0);
	}
	
	/**
	 * 处理请求返回的数据
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == 0) {
			if (data != null) {
				params = data.getExtras();
				requestParamters.clear();
				requestParamters.add(new BasicNameValuePair("product.saleType", saleType));
				for (String key : params.keySet()) {
					String value = params.getString(key);
					if (StringUtils.isNotBlank(value)) {
						if ("keywords".equals(key)) {
							key = "product.productName";
						} else {
							key = key + "s";
						}
						requestParamters.add(new BasicNameValuePair(key, value));
					}
				}
				showProgress();
				new InitDataTask(this, listView).execute(getRequestParams());
			}
		}
	}
}
